import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Author 12629
 * @Date 2022/3/24 16:18
 * @Description：
 */
/*class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
        //return o.age-this.age;
    }
}*/
class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}
class AgeComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o2.age-o1.age;
    }
}

public class TestDemo {

    /**
     * 前K个最小的元素
     * @param array
     */
    public static int[] topK(int[] array,int k) {

        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        //PriorityQueue<Integer> maxHeap2 = new PriorityQueue<>(k,((o1, o2) -> {return o2-o1;}));

        for (int i = 0; i < array.length; i++) {
            if(maxHeap.size() < k) {
                maxHeap.offer(array[i]);
            }else {
                //1、先获取堆顶元素
                int top = maxHeap.peek();
                if(top > array[i]) {
                    maxHeap.poll();
                    maxHeap.offer(array[i]);
                }
            }
        }

        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = maxHeap.poll();
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] array = {1,2,31,4,51,16,7};
        int[] ret = topK(array,3);
        System.out.println(Arrays.toString(ret));
    }


    public static void main4(String[] args) {

        AgeComparator ageComparator = new AgeComparator();

        PriorityQueue<Student> students = new PriorityQueue<>(ageComparator);
        students.offer(new Student("bit",19));
        students.offer(new Student("gaobo",9));
        System.out.println(students);
    }




    public static void main3(String[] args) {
        //默认容量是11  到底是大根堆  还是 小根堆-> 小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(8);
        priorityQueue.offer(30);
        priorityQueue.offer(5);

        System.out.println(priorityQueue.poll());//5
        System.out.println(priorityQueue.peek());//8

    }

    public static void main2(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();

        testHeap.createHeap(array);

        testHeap.heapSort();

        System.out.println("fdsa");

    }
    public static void main1(String[] args) {

        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();

        testHeap.createHeap(array);

        testHeap.push(80);

        System.out.println(testHeap.pop());

        System.out.println("hello");
    }
}
